package com.tude._07vehicle.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tude._07vehicle.base.response.JsonResult;
import com.tude._07vehicle.base.response.StatusCode;
import com.tude._07vehicle.mapper.VehicleMapper;
import com.tude._07vehicle.pojo.dto.VehicleAddParam;
import com.tude._07vehicle.pojo.dto.VehicleListQuery;
import com.tude._07vehicle.pojo.entity.User;
import com.tude._07vehicle.pojo.entity.Vehicle;
import com.tude._07vehicle.pojo.vo.UserVO;
import com.tude._07vehicle.pojo.vo.VehicleListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "02.车辆模块")
@Validated
@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult addVehicle(@RequestBody VehicleAddParam param, @ApiIgnore HttpSession session){
        log.debug("车辆信息：" + param);
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 校验登录的代码在拦截器中
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(param, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());

        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsonResult VehicleList(VehicleListQuery query, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 校验登录的代码在拦截器中
        String brand = query.getBrand();
        String license = query.getLicense();
        Long userId = userVO.getId();
        List<VehicleListVO> list = vehicleMapper.VehicleList(brand, license, userId);
        return JsonResult.ok(list);
    }

    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @PostMapping("del/{id}")
    public JsonResult delVehicle(@PathVariable @Range(min = 1, max = 100, message = "车辆编号必修在1-100之间") Long id,
                                 @ApiIgnore HttpSession session){
        if (id < 0) {
            throw new IllegalArgumentException("id值非法!");
        }
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 校验登录的代码在拦截器中
        Long userId = userVO.getId();
        int i = vehicleMapper.deleteVehicle(id, userId);
        return i > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
